/**
 * Created by Freeman on 2017/11/3.
 */
import {applyMiddleware, compose, createStore} from 'redux'
import thunk from 'redux-thunk'
import {createLogger} from 'redux-logger'
import {persistCombineReducers, persistStore} from 'redux-persist'
import normalizrMiddleware from './middlewares/normalizrMiddleware'
import rootReducers from './root-reducer'
import config from '../config/ReduxPersistConfig'

const middleware = [thunk, normalizrMiddleware]

if (__DEV__) {
  middleware.push(createLogger())
}

const reducers = persistCombineReducers(config, rootReducers);

const enhancers = [applyMiddleware(...middleware)];

const initialState = {};
const persistConfig = { enhancers };

export const store = createStore(reducers, initialState, compose(...enhancers));

const persistor = persistStore(store, persistConfig, () => {
  console.log(store.getState());
});

const configureStore = () => {
  return { persistor, store };
}

export default configureStore;
